﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
using EntLibContrib.Query.Properties;

namespace EntLibContrib.Query.Configuration.Manageability.Queries
{
	/// <summary>
	/// Represents the behavior required to provide Group Policy updates and to publish the 
	/// <see cref="ConfigurationSetting"/> instances associated to a <see cref="QueryData"/>.
	/// </summary>
	public abstract class QueryDataManageabilityProvider<T>
		: ConfigurationElementManageabilityProviderBase<T>
		where T : QueryData
	{
		#region Constants
		/// <summary>
		/// Name for the custom command name property.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
		public const string CustomCommandNameKeyName = "customCommandName";
		/// <summary>
		/// Name for the parameter set name property.
		/// </summary>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
		public const string ParameterSetNameKeyName = "parameterSetName";
		#endregion

		#region Properties
		/// <summary>
		/// Gets the template for the name of the policy associated to the object.
		/// </summary>
		protected sealed override string ElementPolicyNameTemplate
		{
			get { return InternalResources.QueryPolicyNameTemplate; }
		}
		#endregion

		#region Protected Static methods
		/// <summary>
		/// Adds the part to edit the policy overrides for the custom command of a query to the content
		/// built by the <paramref name="contentBuilder"/>.
		/// </summary>
		/// <param name="contentBuilder">The builder for the content where the part will be added.</param>
		/// <param name="partName">Name of the part.</param>
		/// <param name="customCommandName">Name of the custom command.</param>
		/// <param name="configurationSource">The configuration source.</param>
		/// <param name="elementPolicyKeyName">Name of the element policy key.</param>
		protected internal static void AddCustomCommandNamePart(AdmContentBuilder contentBuilder,
																														string partName,
																														string customCommandName,
																														IConfigurationSource configurationSource,
																														string elementPolicyKeyName)
		{
			var configurationSection = (QuerySettings)configurationSource.GetSection(QuerySettings.SectionName);

			contentBuilder.AddDropDownListPartForNamedElementCollection<CustomCommandData>(partName,
																																										elementPolicyKeyName,
																																										CustomCommandNameKeyName,
																																										configurationSection.Commands,
																																										customCommandName,
																																										true);
		}

		/// <summary>
		/// Adds the part to edit the policy overrides for the parameter set of a query to the content
		/// built by the <paramref name="contentBuilder"/>.
		/// </summary>
		/// <param name="contentBuilder">The builder for the content where the part will be added.</param>
		/// <param name="parameterSetName">Name of the parameter set.</param>
		/// <param name="configurationSource">The configuration source.</param>
		/// <param name="elementPolicyKeyName">Name of the element policy key.</param>
		protected internal static void AddParameterSetNamePart(AdmContentBuilder contentBuilder,
																													string parameterSetName,
																													IConfigurationSource configurationSource,
																													string elementPolicyKeyName)
		{
			var configurationSection = (QuerySettings)configurationSource.GetSection(QuerySettings.SectionName);

			contentBuilder.AddDropDownListPartForNamedElementCollection<ParameterSetData>(InternalResources.QueryParameterSetPartName,
																																										elementPolicyKeyName,
																																										ParameterSetNameKeyName,
																																										configurationSection.ParameterSets,
																																										parameterSetName,
																																										true);
		}

		/// <summary>
		/// Utility method that retrieves values from registry keys associated to Group Policy overrides
		/// for the custom command name, giving priority to the machine level key.
		/// </summary>
		/// <param name="policyKey">The registry key that holds the policy overrides.</param>
		/// <returns>
		/// The value for the custom command name on <paramref name="policyKey"/>.
		/// </returns>
		protected internal static string GetCustomCommandNamePolicyOverride(IRegistryKey policyKey)
		{
			string overrideValue = policyKey.GetStringValue(CustomCommandNameKeyName);

			return AdmContentBuilder.NoneListItem.Equals(overrideValue) ? string.Empty : overrideValue;
		}

		/// <summary>
		/// Utility method that retrieves values from registry keys associated to Group Policy overrides
		/// for the parameter set name, giving priority to the machine level key.
		/// </summary>
		/// <param name="policyKey">The registry key that holds the policy overrides.</param>
		/// <returns>
		/// The value for the parameter set name on <paramref name="policyKey"/>.
		/// </returns>
		protected internal static string GetParameterSetNamePolicyOverride(IRegistryKey policyKey)
		{
			string overrideValue = policyKey.GetStringValue(ParameterSetNameKeyName);

			return AdmContentBuilder.NoneListItem.Equals(overrideValue) ? string.Empty : overrideValue;
		}
		#endregion
	}
}
